/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.velocityme.entity;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.persistence.Entity;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;

/**
 *
 * @author rcrida
 */
@Entity()
@Table(name="xuser")
@NamedQueries( {
    @NamedQuery(name="findAllUsers",
                query="SELECT o FROM User o WHERE o.deleted = FALSE"),
    @NamedQuery(name="findUserByName",
                query="SELECT o FROM User o WHERE o.name = :name"),
    @NamedQuery(name="findUserGroups",
                query="SELECT g FROM Group g, IN (g.users) u WHERE u = :user AND g.deleted = FALSE")
})
public class User extends Node {
    private static final long serialVersionUID = 1L;

    private String passwordHash;

    private boolean enabled;

    private boolean showBookmarks;

    @OneToMany(mappedBy = "user")
    private List<Login> logins = new ArrayList<Login>();

    @ManyToOne
    private Person person;

    @OneToMany(mappedBy = "user")
    private Set<UserAccess> userAccesses = new HashSet<UserAccess>();

    public User() {
    }

    public User(Node parentNode, String name, String description, Person person, boolean enabled, boolean showBookmarks) {
        super(parentNode, name, description);
        this.person = person;
        person.getUsers().add(this);
        this.enabled = enabled;
        this.showBookmarks = showBookmarks;
    }
    
    public boolean isEnabled() {
        return enabled;
    }

    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }

    public String getPasswordHash() {
        return passwordHash;
    }

    public void setPasswordHash(String passwordHash) {
        this.passwordHash = passwordHash;
    }

    public Person getPerson() {
        return person;
    }

    public void setPerson(Person person) {
        this.person = person;
        person.getUsers().add(this);
    }

    public boolean isShowBookmarks() {
        return showBookmarks;
    }

    public void setShowBookmarks(boolean showBookmarks) {
        this.showBookmarks = showBookmarks;
    }

    public Set<UserAccess> getUserAccessesUser() {
        return userAccesses;
    }

    public List<Login> getLogins() {
        return logins;
    }

    public String getPersonUserName() {
        return person.getName() + ":" + getName();
    }

}
